package org.dormi.learn.utils;

import org.dormi.learn.definition.Edge;
import org.dormi.learn.definition.Graph;
import org.dormi.learn.definition.Vertex;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : dormi330
 * @since : 2021/1/27/0027
 */
public class GraphGenerator {

    public static Graph v1() {
        Vertex v1 = new Vertex("v1");
        Vertex v2 = new Vertex("v2");
        Vertex v3 = new Vertex("v3");
        Vertex v4 = new Vertex("v4");

        Graph g = new Graph();
        g.addEdge(v1, v2);
        g.addEdge(v2, v3);
        g.addEdge(v1, v4);
        g.setEntry(v1);
        return g;
    }

    public static Graph v2() {
        Graph g = new Graph();
        g.addEdge(fromString("v1,v2,v5"));
        g.addEdge(fromString("v2,v3"));
        g.addEdge(fromString("v5,v6"));
        g.addEdge(fromString("v6,v2,v7"));
        g.addEdge(fromString("v7,v3"));

        g.setEntry(new Vertex("v1"));
        return g;
    }

    public static Graph v3() {
        Graph g = new Graph();
        g.addEdge(fromString("v0,v1"));
        g.addEdge(fromString("v1,v2, v3"));
        g.addEdge(fromString("v3,v4,v0"));
        g.addEdge(fromString("v5,v6"));
        g.addEdge(fromString("v6,v3"));

        g.setEntry(new Vertex("v0"));
        return g;
    }

    public static Graph v4() {
        Graph g = new Graph();
        g.addEdge(fromString("v0,v1"));
        g.addEdge(fromString("v1,v2, v3"));
        g.addEdge(fromString("v3,v4,v0"));
        g.addEdge(fromString("v4,v8"));
        g.addEdge(fromString("v5,v6"));
        g.addEdge(fromString("v6,v3,v7"));
        g.addEdge(fromString("v7,v4,v8"));
        g.addEdge(fromString("v8,v3"));

        g.setEntry(new Vertex("v5"));
        return g;
    }
    /** v1, v2, v3, v4 => v1->v2, v1->v3, v1->v4 */
    private static List<Edge> fromString(String s) {
        String[] allV = s.split(",");
        List<Edge> edges = new ArrayList<>(allV.length - 1);
        List<Vertex> vertices = new ArrayList<>(allV.length);
        for (String v : allV) {
            vertices.add(new Vertex(v.trim()));
        }

        Vertex v0 = vertices.get(0);
        for (int i = 1; i < vertices.size(); i++) {
            edges.add(new Edge(v0, vertices.get(i)));
        }
        return edges;
    }

}
